package data;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;

import database.ConnexionDB;

import network.Connexion;

public class ModeleChrono implements Observer{
	JLabel chronolabel;
	Connexion con;
	Authentification auth;
	ConnexionDB DB;
	ModeleDynamiqueObjetAlerte mdoa = new ModeleDynamiqueObjetAlerte(DB);
	
	public ModeleChrono(JLabel chronolabel, Connexion c, ConnexionDB DB){
		this.chronolabel = chronolabel;
		this.con = c;
		this.DB = DB;
	}
	@Override
	public void update(Observable o, Object arg) {
		Chrono c = (Chrono) o;
		Temps t = c.getChrono();
		this.chronolabel.setText(t.toString());
		updateDB(c);
		con.send(t);
		
		//System.out.println("[Admin][ModeleChrono] set chrono to "+ t.conversion() +" secondes");
	}
	
	public void updateDB(Chrono c){
		if(c.getChrono().conversion() <= 0){
			// partie finie
		}
		else{
			try {
				DB.insertLignePartie(c.getChrono().conversion(),c.is_pause());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
